package com.example.rabbitspringamqp.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

/**
 * @author haitao
 * @date 2021/7/12 1:46 下午
 */
@Configuration
public class RabbitMQConfig {


    public final static String DIRECT_EXCHANGE = "test_direct_exchange";

    public final static String TOPIC_EXCHANGE = "test_topic_exchange";

    public final static String FANOUT_EXCHANGE = "test_fanout_exchange";

    public final static String HEADERS_EXCHANGE = "test_headers_exchange";


    public final static String DIRECT_QUEUE = "test_direct_queue";

    public final static String TOPIC_QUEUE = "test_topic_queue";

    public final static String FANOUT_QUEUE = "test_fanout_queue";

    public final static String HEADERS_QUEUE = "headers_queue";


    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public ConnectionFactory connectionFactory() {
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
        cachingConnectionFactory.setAddresses("10.80.20.209:5672");
        cachingConnectionFactory.setVirtualHost("abm-order");
        cachingConnectionFactory.setUsername("dc_shop");
        cachingConnectionFactory.setPassword("JF8tga3#s3");
        return cachingConnectionFactory;
    }


    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange(DIRECT_EXCHANGE);
    }

    @Bean
    public Queue directQueue() {
        return new Queue(DIRECT_QUEUE, false);
    }

    @Bean
    public Binding directBinding() {
        return new Binding(DIRECT_QUEUE, Binding.DestinationType.QUEUE, DIRECT_EXCHANGE, "direct", new HashMap<>());
    }


    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(TOPIC_EXCHANGE);
    }

    @Bean
    public Queue topicQueue() {
        return new Queue(TOPIC_QUEUE, false);
    }

    @Bean
    public Binding topicBinding() {
        return new Binding(TOPIC_QUEUE, Binding.DestinationType.QUEUE, TOPIC_EXCHANGE, "topic.#", new HashMap<>());
    }


    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(FANOUT_EXCHANGE);
    }

    @Bean
    public Queue fanoutQueue() {
        return new Queue(FANOUT_QUEUE, false);
    }

    @Bean
    public Binding fanoutBinding() {
        return new Binding(FANOUT_QUEUE, Binding.DestinationType.QUEUE, FANOUT_EXCHANGE, "", new HashMap<>());
    }


    @Bean
    public HeadersExchange headersExchange() {
        return new HeadersExchange(HEADERS_EXCHANGE);
    }

    @Bean
    public Queue headersQueue() {
        return new Queue(HEADERS_QUEUE, false);
    }

    @Bean
    public Binding headersBinding() {
        Map<String, Object> map = new Hashtable<>();
        map.put("x-match", "any");
        map.put("user", "zht");
        map.put("code", "java");
        return new Binding(HEADERS_QUEUE, Binding.DestinationType.QUEUE, HEADERS_EXCHANGE, "", map);
    }


}
